Java JavaScript Python C# C C++ Go Kotlin PHP Swift R Ruby TypeScript Scala SQL Perl rust VisualBasic Matlab Julia

Python if else

Nested if

Nested if statements mean an if statement inside another if statement. Here is the basic syntax:

Syntax

Nested if syntax if condition1: # Executes when condition1 is true if condition2: # Executes when condition1 and condition2 are true
Explanation : If condition1 is True, Python checks condition2. If both condition1 and condition2 are True, Python executes the nested code block. If condition1 is False, Python skips the nested if statement.

Basic example

Python nested if basic example x = 10 y = 5 if x == 10: if y == 5: print("x is 10 and y is 5")

Output

x is 10 and y is 5
Explanation : In this example, Python first checks if x is 10. If x is 10, Python checks if y is 5. If both conditions are True, Python prints “x is 10 and y is 5”. Nested if statements can also include elif and else statements:
Nested if example with elif in python x = 10 y = 5 if x == 10: if y == 5: print("x is 10 and y is 5") elif y == 6: print("x is 10 and y is 6") else: print("x is 10 and y is not 5 or 6") else: print("x is not 10")

Output

x is 10 and y is 5
Explanation : In this example, Python first checks if x is 10. If x is 10, Python checks the nested if statement. If y is 5, Python prints “x is 10 and y is 5”. If y is 6, Python prints “x is 10 and y is 6”. If y is neither 5 nor 6, Python prints “x is 10 and y is not 5 or 6”. If x is not 10, Python skips the nested if statement and prints “x is not 10”.
Note : Remember, Python executes only the first True condition in an if-elif-else chain. If an if or elif condition is True, Python skips the rest of the chain. The else clause is a catch-all for any condition not caught by the preceding conditions.
here are some examples to understand nested if statements in Python:

Checking if a number is positive and even:

Checking if a number is positive and even using if else in python num = 10 if num > 0: if num % 2 == 0: print("Number is positive and even") else: print("Number is positive but not even") else: print("Number is not positive")

Output

Number is positive and even

Checking if a person is eligible to vote and to drive:

Checking person is eligible to vote and drive or not in python age = 20 if age >= 18: print("Person is eligible to vote") if age >= 16: print("Person is also eligible to drive") else: print("Person is not eligible to vote or drive")

Output

Person is eligible to vote Person is also eligible to drive

Checking grades:

Finding grade of a student by marks using if else in python grade = 85 if grade >= 60: if grade >= 70: if grade >= 80: if grade >= 90: print("A") else: print("B") else: print("C") else: print("D") else: print("F")

Output

B

Checking if a number is divisible by 2 and 3:

Checking if a number is divisible by 2 and 3 or not num = 6 if num % 2 == 0: if num % 3 == 0: print("Number is divisible by 2 and 3") else: print("Number is divisible by 2 but not by 3") else: print("Number is not divisible by 2")

Output

Number is divisible by 2 and 3

Checking if a point is within a rectangle:

Checking if a point is within a rectangle in python x, y = 5, 5 # Point coordinates if 0 <= x <= 10: # Check if x is within the rectangle if 0 <= y <= 10: # Check if y is within the rectangle print("Point is within the rectangle") else: print("Point is outside the rectangle") else: print("Point is outside the rectangle")

Output

Point is within the rectangle

Lets see about match case as next topic.

  📌TAGS

★python ★ if else ★ loop

Tutorials